package com.heaven.jane.sourcegenerator.business.${name}.controller;

import java.util.Date;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.heaven.jane.sourcegenerator.business.${name}.domain.${name?capitalize};
import com.heaven.jane.sourcegenerator.business.${name}.domain.${name?capitalize}Vo;
import com.heaven.jane.sourcegenerator.business.${name}.service.${name?capitalize}Service;
import com.heaven.jane.sourcegenerator.commons.constant.ResultCode;
import com.heaven.jane.sourcegenerator.commons.exception.BusinessException;
import com.heaven.jane.sourcegenerator.commons.response.ResponseResult;

/**
 * 项目管理
 * @author heaven
 *
 */
@RestController
@RequestMapping("${name}")
public class ${name?capitalize}Controller {
	
	@Autowired
	private ${name?capitalize}Service ${name}Service;
	
	/**
	 * 查询项目列表
	 * @param condition
	 * @return
	 * @throws BusinessException
	 */
	@GetMapping(path = "list")
	public ResponseResult list(${name?capitalize}Vo condition) throws BusinessException {
		Page<${name?capitalize}> list = ${name}Service.list(Optional.ofNullable(condition), PageRequest.of(0, 20));
		if(Optional.ofNullable(list).isPresent()) {
			return ResponseResult.builder().data(list).build();
		}else {
			throw BusinessException.build(ResultCode.AUTH_FAIL.getCode());
		}
	}
	
	@PostMapping(path = "create", consumes = "application/json")
	public ResponseResult create(@RequestBody ${name?capitalize}Vo data) throws BusinessException {
		data.setCreateDate(new Date());
		data.setUpdateDate(new Date());
		data.setAuthor("heaven");
		${name?capitalize} ${name} = ${name}Service.save(data);
		return ResponseResult.builder().data(${name}).build();
	}
	
	@PostMapping(path = "update", consumes = "application/json")
	public ResponseResult update(@RequestBody ${name?capitalize}Vo data) throws BusinessException {
		data.setUpdateDate(new Date());
		${name?capitalize} ${name} = ${name}Service.save(data);
		return ResponseResult.builder().data(${name}).build();
	}
	
	@PostMapping(path = "delete", consumes = "application/json")
	public ResponseResult delete(@RequestBody ${name?capitalize}Vo data) throws BusinessException {
		${name}Service.delete(data);
		return ResponseResult.builder().build();
	}

}
